﻿using AutoMapper;
using MediatR;
using Microsoft.Identity.Client.Extensions.Msal;
using Production_Set.API.Application.Commands.Orderers;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Order;
using THE_WIND_RISES.Common.Domans.Warehousing;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Warehousings
{
    //入库表修改
    public class StorageUpdateHandler : IRequestHandler<StorageUpdateCommend, Result>
    {
        private readonly IBaseRepository<THE_WIND_RISES.Common.Domans.Warehousing.Storage> Storage;
        private readonly IMapper mapper;

        public StorageUpdateHandler(IBaseRepository<THE_WIND_RISES.Common.Domans.Warehousing.Storage> Storage, IMapper mapper)
        {
            this.Storage= Storage;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(StorageUpdateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<THE_WIND_RISES.Common.Domans.Warehousing.Storage>(request);
            string sql = "UPDATE Storage SET StorageNo=@StorageNo ,StorageType=@StorageType ,WarehouseId=@WarehouseId ,StorageDate=@StorageDate " +
                         ",StorageRemark=@StorageRemark,PurchasingTypeId=@PurchasingTypeId,PurchasinId=@PurchasinId,QualityState=@QualityState" +
                         ",StorageState=@StorageState,MakeSingle=@MakeSingle,MakeDepartment=@MakeDepartment WHERE StorageId =@StorageId";
            var list = Storage.Update(sql, new
            {
                StorageId = AddData.StorageId,
                StorageNo = AddData.StorageNo,
                StorageType = AddData.StorageType,
                WarehouseId = AddData.WarehouseId,
                StorageDate = AddData.StorageDate,
                StorageRemark = AddData.StorageRemark,
                PurchasingTypeId = AddData.PurchasingTypeId,
                PurchasinId = AddData.PurchasinId,
                QualityState = AddData.QualityState,
                StorageState = AddData.StorageState,
                MakeSingle = AddData.MakeSingle,
                MakeDepartment = AddData.MakeDepartment,

            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }

            return results;
        }
    }
}
